#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::weak_ptr;
using std::shared_ptr;

void test0(){
    shared_ptr<int> sp(new int(10));
    weak_ptr<int> wp;
    //不能这样创建
    /* weak_ptr<int> wp2(new int(20)); */

    //赋值
    wp = sp;
    shared_ptr<int> sp2 = sp;
    cout << wp.use_count() << endl;
    cout << sp.use_count() << endl;
    cout << sp2.use_count() << endl;

    if(sp){
        cout << "有一片空间" << endl;
    }

    //不允许
    /* if(wp){} */
    cout << *sp << endl;
    /* cout << *wp << endl; */

    shared_ptr<int> sp3 = wp.lock();
    if(sp3){
        cout << "提升成功" << endl;
        cout << *sp3 << endl;
    }else{
        cout << "提升失败，没有托管的空间" << endl;
    }

    cout << endl;
    //expired()返回true代表没有空间被托管
    //返回false代表有空间被托管
    if(!wp.expired()){
        cout << "有空间" << endl;
    }else{
        cout << "没有空间" << endl;
    }
}

int main(void){
    test0();
    return 0;
}
